﻿using e_commerce.BLL.Services;
using e_commerce.Models;
using PagedList;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using e_commerce.Filter;
using e_commerce.BLL.Models;

namespace e_commerce.Controllers
{
    [PermissionFilter(permission: Permission.Comment)]
    public class CommentController : Controller
    {
        // GET: Comment

        private readonly CommentManager _cmanager;

        public CommentController()
        {
            _cmanager = new CommentManager();
            TempData["f-nav"] = "评论管理";
        }

        [PermissionFilter(permission: Permission.Comment_All)]
        public ActionResult AllComment(int page = 1)
        {
            SetTempData("评论管理", "全部评论");
            int pageSize = 8;
            List<CommentModel> list = _cmanager.GetCommentByPage(page, pageSize)
                .Select(c => new CommentModel()
                {
                    CommentId = c.CommentId,
                    CommentRank = c.Rank,
                    CommentDetail = c.CommentDetail,
                    CreateTime = c.CommentTime,
                    GoodsId = c.GoodsInfo.GoodsId,
                    GoodsName = c.GoodsInfo.GoodsName,
                }).ToList();
            return View(list.ToPagedList(page, pageSize));
        }

        /// <summary>
        /// 回复评论
        /// </summary>
        /// <param name="commentId">评论id</param>
        /// <returns></returns>
        public JsonResult ReplyComment(int commentId)
        {
            return null;
        }

        [HttpPost]
        public ActionResult GetNewComment()
        {
            TempData["url"] = "/Comment/AllComment";
            TempData["icon"] = "/Image/icon_newcomment.png";
            TempData["name"] = "评论";
            TempData["title"] = "最新评论";
            var orders = _cmanager.GetNewComment(5).Select(o => new AbbreviationsModel()
            {
                CreateTime = o.CommentTime.ToString(),
                Name = o.CommentDetail,
            });
            return PartialView("~/Views/Admin/ViewComponents/NewTips.cshtml", orders);
        }

        private void SetTempData(string fnav,string snav)
        {
            TempData["f-nav"] = fnav;
            TempData["s-nav"] = snav;
        }
    }
}